<template>
  <div>
    <!--
        form:
          inline,它的属性值为true，代表行内form，一行可以放置多个表单
        el-option
           label 下拉菜单展示文字
           value  将来是select要收集数据
      -->
    <el-form :inline="true" class="demo-form-inline" :model="cForm">
      <el-form-item label="一级分类">
        <el-select
          placeholder="请选择"
          v-model="cForm.category1Id"
          @change="getC2List"
        >
          <!-- 遍历一级分类的option -->
          <el-option
            :label="c1.name"
            :value="c1.id"
            v-for="(c1, index) in c1List"
            :key="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          placeholder="请选择"
          v-model="cForm.category2Id"
          @change="getC3List"
        >
          <el-option
            :label="c2.name"
            :value="c2.id"
            v-for="(c2, index) in c2List"
            :key="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select placeholder="请选择" v-model="cForm.category3Id" @change="handler">
          <el-option :label="c3.name" :value="c3.id" v-for="(c3,index) in c3List" :key="c3.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Category",
  data() {
    return {
      //收集数据的地方
      cForm: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
        },
      //存储一级分类的数据
      c1List: [],
      //存储二级分类的数据
      c2List: [],
      //存储三级分类的数据
      c3List: [],
    };
  },
  mounted() {
    //获取一级分类的请求函数
    this.getC1List();
  },
  methods: {
    async getC1List() {
      let result = await this.$ajax.attr.reqGetCategory1();
      if (result.code == 200) {
        this.c1List = result.data;
      }
    },
    //一级分类select的change回调函数,当一级分类的内容发生变化的时候,发请求获取二级分类的数据
    async getC2List() {
      //每一次一级分类发生变化的时候,需要吧二级 三级分类的数据清空
      this.c2List = [];
      this.c3List = [];
      this.cForm.category2Id = "";
      this.cForm.category3Id = "";
      //携带一级分类的id 获取二级分类的数据
      const { category1Id } = this.cForm;
      //通过自定义事件给父组件传递过去
      this.$emit("getCategoryId", { id: category1Id, flag: 1 });
      //获取二级分类数据
      let result = await this.$ajax.attr.reqGetCategory2(category1Id);
      if (result.code == 200) {
        this.c2List = result.data;
      }
    },
    //二级分类select的change回调函数,当二级分类的内容发生变化的时候,发请求获取三级分类的数据
    async getC3List() {
      //清空三级分类的数据+id
      this.c3List = []
      this.cForm.category3Id=''
      //获取二级分类的id
      const {category2Id} = this.cForm
      this.$emit('getCategoryId',{id:category2Id,flag:2})
      //发请求
      let result = await this.$ajax.attr.reqGetCategory3(category2Id)
      if (result.code == 200) {
        this.c3List = result.data;
      }
    },
    //三级分类的chang的事件
    handler(){
      //获取三级分类的id
      const{category3Id} = this.cForm
      //传给父组件
      this.$emit('getCategoryId',{id:category3Id,flag:3})
    }
  },
};
</script>

<style scoped>
</style>




